/// <reference path="Core.js" />

PolyGraph3D.Page = function() 
{
}

PolyGraph3D.Page.prototype =
{
	handleLoad: function(control, userContext, rootElement) 
	{
		this.control = control;
		
		var container = rootElement.FindName("ViewPort");
		this.viewPort = new PolyGraph3D.ViewPort(control, container);
		
		this.viewPort.ViewCamera.set_Location(new PolyGraph3D.Vector(0, 0, 50));
		//this.viewPort.ViewCamera.set_Location(new PolyGraph3D.Vector(25, 25, 50));
		//this.viewPort.ViewCamera.set_Rotation(new PolyGraph3D.Vector(-25, -25, 0));
		
		// create a sample object (40x40 cube)
        var points = new Array();
        points.push(new PolyGraph3D.Point(-20, -20, -20));
        points.push(new PolyGraph3D.Point(-20,  20, -20));
        points.push(new PolyGraph3D.Point( 20,  20, -20));
        points.push(new PolyGraph3D.Point( 20, -20, -20));
        points.push(new PolyGraph3D.Point(-20, -20,  20));
        points.push(new PolyGraph3D.Point(-20,  20,  20));
        points.push(new PolyGraph3D.Point( 20,  20,  20));
        points.push(new PolyGraph3D.Point( 20, -20,  20));
        
        this.vis = new PolyGraph3D.Model.Visual();
        this.vis.set_Points(points);
        var tex = this.vis.DefineColorTexture("#FF0000FF");
        this.vis.CreateFace([4, 5, 6, 7], tex);
        this.vis.CreateFace([7, 6, 2, 3], tex);
        this.vis.CreateFace([3, 2, 1, 0], tex);
        this.vis.CreateFace([0, 1, 5, 4], tex);
        this.vis.CreateFace([6, 5, 1, 2], tex);
        this.vis.CreateFace([4, 7, 3, 0], tex);
		this.vis.set_Location(new PolyGraph3D.Vector(40, 40, 0));
		
		this.viewPort.AddVisual(this.vis);
		this.viewPort.UpdateView();
		
		rootElement.FindName("PlusX").addEventListener("MouseLeftButtonDown", Silverlight.createDelegate(this, this.PlusX));
		rootElement.FindName("MinusX").addEventListener("MouseLeftButtonDown", Silverlight.createDelegate(this, this.MinusX));
		rootElement.FindName("PlusY").addEventListener("MouseLeftButtonDown", Silverlight.createDelegate(this, this.PlusY));
		rootElement.FindName("MinusY").addEventListener("MouseLeftButtonDown", Silverlight.createDelegate(this, this.MinusY));
		rootElement.FindName("PlusZ").addEventListener("MouseLeftButtonDown", Silverlight.createDelegate(this, this.PlusZ));
		rootElement.FindName("MinusZ").addEventListener("MouseLeftButtonDown", Silverlight.createDelegate(this, this.MinusZ));
		
		rootElement.FindName("FramePump").addEventListener("Completed", Silverlight.createDelegate(this, this.FramePump));
		
		var DegPerRadian = 57.2958;
		var DegToTurnPerSecond = 12;
		var RadianPerSecond = DegToTurnPerSecond / DegPerRadian;
		var RadianPerFrame = RadianPerSecond / 24;
		
		this.rotChange = new PolyGraph3D.Vector(0, RadianPerFrame, 0);
		
		rootElement.FindName("Spin").addEventListener("MouseLeftButtonDown", Silverlight.createDelegate(this, this.Spin));
		this.spin = false;
	},
	
	Spin: function(sender, eventArgs) 
	{
	    this.spin = !this.spin;
	    
	    if (this.spin)
		    sender.FindName("FramePump").Begin();
		else
		    sender.FindName("FramePump").Stop();
	},
	
	FramePump: function(sender, eventArgs) 
	{
		sender.FindName("FramePump").Stop();
		
		if (this.spin)
		{
	        var rot = this.vis.get_Rotation();
	        PolyGraph3D.Vector.Combine(rot, this.rotChange);
	        this.vis.set_Rotation(rot);
		    this.viewPort.UpdateView();
    		
		    sender.FindName("FramePump").Begin();
		}
	},
	
	PlusX: function(sender, eventArgs) 
	{
	    var loc = this.vis.get_Location();
	    loc.X -= 10;
	    this.vis.set_Location(loc);
		this.viewPort.UpdateView();
	},
	
	MinusX: function(sender, eventArgs) 
	{
	    var loc = this.vis.get_Location();
	    loc.X += 10;
	    this.vis.set_Location(loc);
		this.viewPort.UpdateView();
	},
	
	PlusY: function(sender, eventArgs) 
	{
	    var loc = this.vis.get_Location();
	    loc.Y += 10;
	    this.vis.set_Location(loc);
		this.viewPort.UpdateView();
	},
	
	MinusY: function(sender, eventArgs) 
	{
	    var loc = this.vis.get_Location();
	    loc.Y -= 10;
	    this.vis.set_Location(loc);
		this.viewPort.UpdateView();
	},
	
	PlusZ: function(sender, eventArgs) 
	{
	    var loc = this.vis.get_Location();
	    loc.Z += 10;
	    this.vis.set_Location(loc);
		this.viewPort.UpdateView();
	},
	
	MinusZ: function(sender, eventArgs) 
	{
	    var loc = this.vis.get_Location();
	    loc.Z -= 10;
	    this.vis.set_Location(loc);
		this.viewPort.UpdateView();
	}
}